package com.mxpio.erp.plm.service.impl;

import com.mxpio.erp.common.enums.CommonEnums;
import com.mxpio.erp.common.enums.OrderStatusEnums;
import com.mxpio.erp.common.plm.entity.*;
import com.mxpio.erp.common.plm.service.ProjectInfoEndService;
import com.mxpio.erp.common.plm.service.ProjectInfoRecordDeliverableDraftService;
import com.mxpio.erp.common.plm.service.ProjectInfoService;
import com.mxpio.erp.common.plm.vo.ProjectInfoDetailVO;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.JpaUtil;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.jpa.query.Operator;
import com.mxpioframework.system.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 项目终止
 */
@Service
public class ProjectInfoEndServiceImpl extends BaseServiceImpl<ProjectInfoEnd> implements ProjectInfoEndService {
    @Autowired
    private ProjectInfoService projectInfoService;
    @Autowired
    private ProjectInfoRecordDeliverableDraftService projectInfoRecordDeliverableDraftService;
    @Override
    @Transactional
    public ProjectInfo getOne(String bizNo) {
        ProjectInfoEnd byId = this.getById(ProjectInfoEnd.class, bizNo);
        ProjectInfo projectInfo = projectInfoService.getById(ProjectInfo.class, byId.getProCode());
        projectInfo.setReason(byId.getReason());
        projectInfoService.update(projectInfo);
        return projectInfo;

//        List<ProjectInfoEnd> list = this.list(ProjectInfoEnd.class, Criteria.create().addCriterion("proCode", Operator.EQ, proCode));
//        for (ProjectInfoEnd end : list){
//            if (end.getBpmnStatus().equals(OrderStatusEnums.BpmnStatus.CREATE.getStatusCode())
//                    ||end.getBpmnStatus().equals(OrderStatusEnums.BpmnStatus.AUDITING.getStatusCode())){
//                ProjectInfo projectInfo = projectInfoService.getById(ProjectInfo.class, proCode);
//                projectInfo.setReason(end.getReason());
//                projectInfoService.update(projectInfo);
//                return projectInfo;
//            }
//        }
//        return null;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result<?> getDetail(String bizNo) {
        ProjectInfoDetailVO projectInfoDetailVO = new ProjectInfoDetailVO();
        ProjectInfoRecordDraft infoRecordDraft = JpaUtil.linq(ProjectInfoRecordDraft.class).equal("bizNo", bizNo).findOne();
        List<ProjectInfoRecordDeliverableDraft> deliverableDrafts = projectInfoRecordDeliverableDraftService.list(ProjectInfoRecordDeliverableDraft.class, Criteria.create().addCriterion("bizNo", Operator.EQ, bizNo));
        projectInfoDetailVO.setProjectInfoRecordDraft(infoRecordDraft);
        projectInfoDetailVO.setProjectInfoRecordDeliverableDrafts(deliverableDrafts);
        return Result.OK(projectInfoDetailVO);
    }
}
